package llvmir.value.instructions;

import java.util.List;

import llvmir.value.Value;
import llvmir.value.structure.BasicBlock;
import llvmir.value.structure.Function;
import llvmir.value.structure.Instruction;

public class Call extends Instruction {
    public Call(Value function, List<Value> args, BasicBlock basicBlock) {
        super(null, function.getType(), basicBlock);
        this.operands.add(function);
        this.operands.addAll(args);
    }

    // 无参数的函数调用
    public Call(Value function, BasicBlock basicBlock) {
        super(null, function.getType(), basicBlock);
        this.operands.add(function);
    }

    // 单个参数的函数调用
    public Call(Value function, Value arg, BasicBlock basicBlock) {
        super(null, function.getType(), basicBlock);
        this.operands.add(function);
        this.operands.add(arg);
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        Function f = (Function) operands.get(0);
        if (!(f.getType().isVoidType())) {
            sb.append(name + " = ");
        }
        sb.append("call " + this.getType() + " ");
        sb.append(f.getName() + "(");
        for (int i = 1; i < operands.size(); i++) {
            sb.append(operands.get(i).getType() + " " + operands.get(i).getName());
            if (i != operands.size() - 1) {
                sb.append(", ");
            }
        }
        sb.append(")");
        return sb.toString();
    }
}
